package com.bawei.a2010douyin.ui.zhibo;

import android.graphics.Color;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.base.mvp.BaseView;
import com.bawei.a2010douyin.entity.ChatEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

public class ChatAdapter extends BaseQuickAdapter<ChatEntity, BaseViewHolder> {
    public ChatAdapter(int layoutResId) {
        super(layoutResId);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ChatEntity chatEntity) {
        //富文本：丰富的文本样式 姚甜雪:你好吗
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(chatEntity.getUsername()+":"+chatEntity.getMessage());
        //设置字体颜色
        ForegroundColorSpan blue = new ForegroundColorSpan(getContext().getColor(R.color.blue));
        ForegroundColorSpan white = new ForegroundColorSpan(getContext().getColor(R.color.white));
        stringBuilder.setSpan(blue,0,chatEntity.getUsername().length()+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        stringBuilder.setSpan(white,chatEntity.getUsername().length()+1,chatEntity.getUsername().length()+1+chatEntity.getMessage().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        //设置富文本
        baseViewHolder.setText(R.id.tv,stringBuilder);
    }
}
